package com.knight.admin.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.knight.admin.common.constant.Constant;
import com.knight.admin.model.Menu;
import com.knight.admin.model.Project;
import com.knight.admin.response.JsonMessage;
import com.knight.admin.service.MenuService;
import com.knight.admin.service.ProjectService;

/**
 * 案例管理
 * 
 * @author YQ
 *
 */
public class ProjectManageController extends Controller {
	private static MenuService menuService = MenuService.getInstance();
	private static ProjectService projectService = ProjectService.getInstance();

	/**
	 * 图片上传目录
	 */
	private static final String UPLOAD_IMAGE_DIRECTORY = Constant.ACCESS_IMAGE_PRE+"project" + File.separator;

	/**
	 * 查询子节点
	 */
	public void childMenu() {
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.PROJECTS_ID);

		Project model = getModel(Project.class);
		String menuId = "";
		if (model.getMenuId() != null && !"".equals(model.getMenuId())) {
			menuId = model.getMenuId();
		} else {
			menuId = list.get(0).getId();
		}

		Page<Project> pageProject = projectService.findProjectByMenuId(getParaToInt("pageIndex", 1), 5, menuId);
		setAttr("childMenus", list);
		setAttr("pageProject", pageProject);
		setAttr("currentMenuId", menuId);
		render("project/project.html");
	}

	/**
	 * 跳转添加project的页面
	 */
	public void adddProjectPage() {
		setAttr("title", "添加案例");
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.PROJECTS_ID);
		setAttr("childMenus", list);
		render("project/project-add.html");
	}

	/**
	 * 保存project方法
	 * 
	 * @throws IOException
	 */
	public void adddProject() throws IOException {

		String uploadPath = UPLOAD_IMAGE_DIRECTORY + DateFormatUtils.format(new Date(), "yyyyMMdd");

		List<UploadFile> files = this.getFiles(uploadPath);

		// 保存到数据的地址
		String saveUploadPuath = uploadPath + File.separator;

		if (CollectionUtils.isNotEmpty(files)) {

			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < files.size(); i++) {
				String path = saveUploadPuath + files.get(i).getFileName();
				if (i == (files.size() - 1)) {
					sb.append(path);
				} else {
					sb.append(path + ",");
				}
			}

			String showUrl = saveUploadPuath + files.get(0).getFileName();

			Project project = getModel(Project.class);

			projectService.saveProject(showUrl, sb.toString(), project);

			redirect("/project/childMenu");
		} else {
			render("project/project-add.html");
		}

		/*
		 * if (files != null && files.size() > 0) { for (int i = 0; i <
		 * files.size(); i++) { String fileName = UUID.randomUUID().toString();
		 * File file = files.get(i).getFile(); if (QiniuServiceImpl.upload(file,
		 * fileName)) { if (i == (files.size() - 1)) { sb.append(fileName); }
		 * else { sb.append(fileName + ","); }
		 * 
		 * if (i == 0) { showUrl = fileName; } }else{ renderError(500); } } }
		 */

	}

	/**
	 * 案例详情
	 */
	public void projectDetail() {
		setAttr("title", "案例详情");
		List<Menu> list = menuService.findMenuByparentId(Constant.MenuTypes.LEVEL_TWO_MENU,
				Constant.ParentMenuId.PROJECTS_ID);
		setAttr("childMenus", list);

		Integer id = getParaToInt(0, 1);
		Project project = projectService.findById(id);
		if (project != null) {
			String urls = project.getUrls();
			List<String> urlList = new ArrayList<String>();
			if (urls.contains(Constant.Symbo.COMMA)) {
				for (String url : urls.split(Constant.Symbo.COMMA)) {
					urlList.add(url);
				}
			} else {
				urlList.add(urls);
			}

			setAttr("urls", urlList);
		}

		setAttr("p", project);

		render("project/project-detail.html");
	}

	/**
	 * 删除案例
	 */
	public void deleteProject() {
		Integer id = getParaToInt(0, 1);
		boolean flag = projectService.deleteById(id);
		JsonMessage message = new JsonMessage();
		if (flag) {
			message.setSuccess("0");
			message.setSuccMessage("删除成功");
		} else {
			message.setSuccess("1");
			message.setErrorMessage("删除失败");
		}
		renderJson(message);
	}
}
